//
//  HTTPClient.m
//  DebugDemo
//
//  Created by XFB on 2018/11/22.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "HTTPClient.h"

static AFHTTPSessionManager *_sessionManager;
static NSMutableArray *_allSessionTask;

@implementation HTTPClient

+ (NSMutableArray *)allSessionTask {
    if (!_allSessionTask) {
        _allSessionTask = [[NSMutableArray alloc] init];
    }
    return _allSessionTask;
}

+ (void)initialize
{
    _sessionManager = [AFHTTPSessionManager manager];
    _sessionManager.requestSerializer.timeoutInterval = 20.0f;
    _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    _sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    /// 请求头添加sign
    NSString *sign = @"";
    [_sessionManager.requestSerializer setValue:sign forHTTPHeaderField:@"sign"];
}

+ (NSURLSessionTask *)getURL:(NSString *)urlString
                   parameter:(id)parameters
                     success:(HTTPRequestSuccessBlock)success
                     failure:(HTTPRequestFailureBlock)failure;
{
//    NSLog(@"1---%ld", [self allSessionTask].count);
    NSURLSessionTask *task = [_sessionManager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"2---%ld", [self allSessionTask].count);
        success(responseObject);
        /// 减少网络请求数目
        [[self allSessionTask] removeObject:task];
//        NSLog(@"3---%ld", [self allSessionTask].count);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"4---%ld", [self allSessionTask].count);
        failure(error);
        /// 减少网络请求数目
        [[self allSessionTask] removeObject:task];
//        NSLog(@"6---%ld", [self allSessionTask].count);
    }];
    
    task ? [[self allSessionTask] addObject:task] : nil ;
//    NSLog(@"6---%ld", [self allSessionTask].count);
    return task;
}

+ (NSURLSessionTask *)postURL:(NSString *)urlString
                    parameter:(id)parameters
                      success:(HTTPRequestSuccessBlock)success
                      failure:(HTTPRequestFailureBlock)failure
{
    NSURLSessionTask *task = [_sessionManager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
        /// 减少网络请求数目
        [[self allSessionTask] removeObject:task];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        /// 减少网络请求数目
        [[self allSessionTask] removeObject:task];
    }];
    task ? [[self allSessionTask] addObject:task] : nil ;
    return task;
}

+ (void)cancelAllRequest
{
//    NSLog(@"7---%ld", [self allSessionTask].count);
    @synchronized (self) {
        [[self allSessionTask] enumerateObjectsUsingBlock:^(NSURLSessionTask *_Nonnull task, NSUInteger idx, BOOL * _Nonnull stop) {
            [task cancel];
        }];
        [[self allSessionTask] removeAllObjects];
//        NSLog(@"8---%ld", [self allSessionTask].count);
    }
//    NSLog(@"9---%ld", [self allSessionTask].count);
}

@end
